package bytes;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Description
 * @Author zhaobaolin
 * @Date 2018/11/10
 */
public class FileInputStreamExample {
    public static void main(String[] args) {
        //create();
        read();
    }

    private static void create()
    {
        try{
            FileInputStream f1 = new FileInputStream("./src/bytes/cn.txt");
            System.out.println(f1);
            File file = new File("./src/bytes/cn.txt");
            FileInputStream f2 = new FileInputStream(file);
            System.out.println(f2);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

    private static void read()
    {
        try(FileInputStream f = new FileInputStream("./src/bytes/en.txt")){
            int size;
            System.out.println("总字节是："+(size = f.available()));

            int num = 3;
            while(num>0){
                System.out.println("单个字节的读："+(char) f.read());
                num--;
            }

            //注意：此时io流的指针已经到了第4个字节了，所以下面打印的内容不包含前3个字节。
            byte b[] = new byte[size];
            f.read(b);
            System.out.println("文件内容是："+new String(b));
            //System.out.println("文件内容是："+new String(b,0,size));//也可以这样



        }catch (IOException e){
            e.printStackTrace();
        }
    }




}
